package com.EJ.ejcompanyweb.Util;

import android.Manifest;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.telecom.Call;
import android.util.Log;

import com.tbruyelle.rxpermissions.Permission;
import com.tbruyelle.rxpermissions.RxPermissions;

import rx.Subscription;
import rx.functions.Action1;

public class GetRxPermissions {
/**
    添加依赖
    implementation 'com.tbruyelle.rxpermissions:rxpermissions:0.7.0@aar'
    implementation 'io.reactivex:rxjava:1.1.3'
    然后在minifast加入所需要的Permissions
    actvity中使用
    new GetRxPermissions(this,new String[]{Manifest.permission.READ_EXTERNAL_STORAGE,Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.INTERNET}).StartRequestPermissions();
    */
    private String TAG=this.getClass().getSimpleName();
    private Activity ActivityCompat;
    private String[] PermissionsMessger={Manifest.permission.READ_EXTERNAL_STORAGE,Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.INTERNET};

    public GetRxPermissions(Activity activityCompat, String[] permissionsMessger) {
        ActivityCompat = activityCompat;
        if (PermissionsMessger!=null){
            PermissionsMessger = permissionsMessger;
        }

    }

    public void StartRequestPermissions(){
        Log.d(TAG,PermissionsMessger.toString());
        //分别申请多个权限
       RxPermissions.getInstance(ActivityCompat)
                //分别申请多个权限时，使用requestEach
                .requestEach(PermissionsMessger)
                .subscribe(new Action1<Permission>() {
                    @Override
                    public void call(Permission permission) {
                        if (permission.name.equals(Manifest.permission.READ_EXTERNAL_STORAGE)) {
                            //当ACCESS_FINE_LOCATION权限获取成功时，permission.granted=true
                            Log.i(TAG, Manifest.permission.READ_EXTERNAL_STORAGE+ "：" + permission.granted);
                        }
                        if (permission.name.equals(Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
                            //当RECORD_AUDIO 权限获取成功时，permission.granted=true
                            Log.i(TAG, Manifest.permission.WRITE_EXTERNAL_STORAGE + "：" + permission.granted);
                        }
                        if (permission.name.equals(Manifest.permission.INTERNET)){
                            //当CAMERA权限获取成功时，permission.granted=true
                            Log.i(TAG, Manifest.permission.INTERNET + "：" + permission.granted);
                        }

                    }
                }

                );
    }

    private void showWaringDialog() {
        AlertDialog dialog = new AlertDialog.Builder(ActivityCompat)
                .setTitle("警告！")
                .setMessage("请前往设置->应用->权限->权限中打开相关权限，否则功能无法正常运行！")
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // 一般情况下如果用户不授权的话，功能是无法运行的，做退出处理
                        ActivityCompat.finish();
                    }
                }).show();
    }
    public void setPermissionsMessger(String[] permissionsMessger) {
        PermissionsMessger = permissionsMessger;
    }
}
